package com.weichat.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.User;
import com.service.Event_userService;
import com.service.UserService;
import com.weichat.tool.WeiChatUtil;

@Controller
public class JumpController {
	@Autowired
	UserService userService;
	@Autowired
	Event_userService event_userService;
	@Value("${defaultURL}")
	String defaultURL;
	
	
	@RequestMapping("Jump")
	public String Jump(HttpServletRequest req){
		String html="";
		String code=req.getParameter("code");
		String openId;
		if(code!=null){
			openId=WeiChatUtil.getOpenId(code);
			req.getSession().setAttribute("openId", openId);
			System.out.println("code=="+code+" ---openId=="+openId);
		}else{
			openId=(String) req.getSession().getAttribute("openId");
		}
		
		
		//获取当前时间
		Calendar now = Calendar.getInstance();
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEEE");  
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE",Locale.ENGLISH);  
		String dateNowStr = sdf.format(now.getTimeInMillis()); 
		SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");  
		String dateNowStr1 = sdf1.format(now.getTimeInMillis()); 
		int hour=Integer.parseInt(dateNowStr1.substring(0,dateNowStr1.indexOf(":")));
		int min=Integer.parseInt(dateNowStr1.substring(dateNowStr1.indexOf(":")+1, dateNowStr1.length()));
		System.out.println("小时==="+hour+"分钟===="+min);
		
		if((userService.selectByOpenId(openId).size()==0)&&!(openId.equals(""))){
			html="register.html";
		}else if(dateNowStr.equals("Monday")||dateNowStr.equals("Wednesday")||dateNowStr.equals("Friday")){
			User u=userService.selectByOpenId(openId).get(0);
			req.getSession().setAttribute("user", u);
	        	if((hour>=7&&min>=00)&&(hour<=7&&min<30)){
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==1)){
	        			html="replace.html";
	        		}else{
	        			html="getup-wsq.html";
	        		}
	        		//System.out.println("早上起床按钮");
	        	}else if((hour>=7&&min>=30)&&(hour<=7&&min<=49)){
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==2)){
	        			html="replace.html";
	        		}else{
	        			html="eatbreakfast-wsq.html";
	        		}
	        		//System.out.println("吃早餐按钮");
	        	}else if((hour>=7&&min>=55)&&(hour<=8&&min<00)){
	        		System.out.println("乘营车按钮");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==3)){
	        			html="replace.html";
	        		}else{
	        			html="TakeBus2.html";
	        		}
	        		
	        	}else if((hour>=8&&min>=00)&&(hour<=8&&min<30)){
	        		//jwh
	        	
	        	 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==4)){
	        			html="replace.html";
	        		}else{
	        			 html="setPhone-jwh.html";
	        		}
	        	}else if((hour>=8&&min>=30)&&(hour<=9&&min<01)&&(true)){
	        		System.out.println("true的条件是没有乘坐营车");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==4)){
	        			html="replace.html";
	        		}else{
	        			 html="setPhone-jwh.html";
	        		}
	        		System.out.println("早上存放手机");
	        	}else if((hour>=13&&min>=00)&&(hour<=13&&min<20)){
	        		System.out.println("午睡按钮");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==5)){
	        			html="replace.html";
	        		}else{
	        			html="Siesta.html";
	        		}
	        	}else if((hour>=13&&min>=50)&&(hour<=14&&min<00)){
	        		//jwh
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==6)){
	        			html="replace.html";
	        		}else{
	        			html="breakUp-jwh.html";
	        		}
	        	
	        	}else if((hour>=14&&min>=00)&&(hour<=14&&min<=15)){
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==7)){
	        			html="replace.html";
	        		}else{
	        			html="setPhone-hc.html";
	        		}
	        		System.out.println("下午存放手机");
	        		
	        			
	        	}else if((hour>=17&&min>=00)&&(hour<=18&&min<=45)){
	        		//jwh
	        		html="run.html";
	        		System.out.println("环湖跑开始按钮");
	        		System.out.println("天气原因按钮");
	        	}else if((hour>=19&&min>=30)&&(hour<=19&&min<59)){
	        		System.out.println("晚上存放手机");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==11)){
	        			html="replace.html";
	        		}else{
	        			html="setPhone-lsw.html";
	        		}
	        		
	        	}else if((hour>=23&&min>=30)&&(hour<=23&&min<51)){
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==12)){
	        			html="replace.html";
	        		}else{
	        			html="darksleep-wsq.html";
	        		}
	        		
	        		//System.out.println("上床睡觉按钮");
	        	}else {
	        		System.out.println("其他");
	        		html="else.html";
	        	}
	        	
	        }else if(dateNowStr.equals("Saturday")||dateNowStr.equals("Sunday")){
	        	User u=userService.selectByOpenId(openId).get(0);
				req.getSession().setAttribute("user", u);
	        	if((hour>=7&&min>=00)&&(hour<=7&&min<30)){
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==1)){
	        			html="replace.html";
	        		}else{
	        			html="getup-wsq.html";
	        		}
	        		
	        		//System.out.println("早上起床按钮");
	        	}else if((hour>=7&&min>=30)&&(hour<=7&&min<=49)){
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==2)){
	        			html="replace.html";
	        		}else{
	        			html="eatbreakfast-wsq.html";
	        		}
	        		
	        	//	System.out.println("吃早餐按钮");
	        		System.out.println("营车动态位置");
	        	}else if((hour>=7&&min>=55)&&(hour<=8&&min<00)){
	        		System.out.println("乘营车按钮");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==3)){
	        			html="replace.html";
	        		}else{
	        			html="TakeBus2.html";
	        		}
	        		
	        	}else if((hour>=8&&min>=00)&&(hour<=8&&min<30)){
	        		//jwh
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==4)){
	        			html="replace.html";
	        		}else{
	        			html="setPhone-jwh.html";
	        		}
		        	 
	        	}else if((hour>=8&&min>=30)&&(hour<=9&&min<01)&&(true)){
	        		System.out.println("true的条件是没有乘坐营车");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==4)){
	        			html="replace.html";
	        		}else{
	        			html="setPhone-jwh.html";
	        		}
	        		System.out.println("早上存放手机");
	        	}else if((hour>=13&&min>=00)&&(hour<=13&&min<20)){
	        		System.out.println("午睡按钮");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==5)){
	        			html="replace.html";
	        		}else{
	        			html="Siesta.html";
	        		}
	        		
	        	}else if((hour>=13&&min>=50)&&(hour<=13&&min<55) ){
	          		//jwh
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==6)){
	        			html="replace.html";
	        		}else{
	        			html="breakUp-jwh.html";
	        		}
		        	
	        	}else if((hour>=14&&min>=00)&&(hour<=14&&min<=15)){
	        		
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==7)){
	        			html="replace.html";
	        		}else{
	        			html="setPhone-hc.html";
	        		}
	        		System.out.println("下午存放手机");
	        	}else if((hour>17&&min>00)&&(hour<18&&min<45)){
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==9)){
	        			html="replace.html";
	        		}else{
	        			html="sports.html";
	        		}
	        		System.out.println("周末室外锻炼");
	        	}else if((hour>=18&&min>=30)&&(hour<=18&&min<59)){
	        		System.out.println("晚上存放手机");
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==11)){
	        			html="replace.html";
	        		}else{
	        			html="setPhone-lsw.html";
	        		}
	        		
	        	}else if((hour>=23&&min>=30)&&(hour<=23&&min<51)){
	        		
	        		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); 
	        		if((sd.format(now.getTimeInMillis()).equals(sd.format(event_userService.selectEidByUidOrder(u.getId()).getUpdateDt().getTime())))&&(event_userService.selectEidByUidOrder(u.getId()).getEventId()==12)){
	        			html="replace.html";
	        		}else{
	        			html="darksleep-wsq.html";
	        		}
	        		//System.out.println("上床睡觉按钮");
	        	}else{
	        		//jwh
	        		System.out.println("eeeeeeeeeeeee");
	        			html="else.html";
	        	}
	        	
	        	
	        }else if(dateNowStr.equals("Tuesday")||dateNowStr.equals("Thursday")){
	        	User u=userService.selectByOpenId(openId).get(0);
				req.getSession().setAttribute("user", u);
				html="free.html";
	        	
	        }
		if(!("N".equals(defaultURL))){
			html=defaultURL;
		}
		System.out.println("html======"+html+"---defaultURL----"+defaultURL);
		 //html="sports.html";
		return html;
	}

}
